#ifndef MANAGEMENT_H
#define MANAGEMENT_H

#include <QWidget>
#include <QMouseEvent>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStandardItemModel>
#include"clientconfig.h"
#include"tcpsocket.h"
namespace Ui {
class management;
}

typedef struct BaseModel
{
    QJsonObject head;
    QJsonArray body;
    int model;
    BaseModel(QByteArray data) {
        QJsonDocument row = QJsonDocument::fromJson(data);
        QJsonObject localObject = row.object();
        head = localObject.value("head").toObject();
        body = localObject.value("body").toArray();
        model = head.value("model").toInt();
    }
}baseMode;

typedef struct Mode1: BaseModel
{
    int err;
    QJsonArray field;
    int m_body_size=0;
    int m_filed_size=0;
    Mode1(QByteArray data): BaseModel(data)
    {
        err = head.value("err").toInt();
        // 成功err==0
        if (err == 0)
        {
            field = head.value("field").toArray();
            m_filed_size = field.size();
        }
    }
}model1;

typedef struct Mode2: BaseModel
{
    int err;
    Mode2(QByteArray data): BaseModel(data)
    {
        err = head.value("err").toInt();
        // 成功err==0
    }
}model2;

class management : public QWidget
{
    Q_OBJECT

public:
    explicit management(QWidget *parent = 0);
    ~management();
    void setBackground();
    // 模式1发送获取展示数据
    QString sendModel1();
    // 解析模式1
    bool parseModel1(QByteArray rowData);
    // 模式2发送数据
    QString sendModel2(QStandardItemModel*& model);
    bool parseModel2(QByteArray rowData);
    // 模式3发送数据
    QString sendModel3(QString id);
    // 模式4发送
    QString sendModel4(QString user, QString pwd);
    // 表格字段
    QStringList m_strList;
    // 常驻显示模型
    QStandardItemModel* item_mode=nullptr;
    TcpSocket *tcp_client; // tcp课户端
protected:
    void mouseReleaseEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
private:
    Ui::management *ui;
    QPoint mousePoint;  // 记录鼠标坐标
    bool mouse_press;  // 记录是否聚焦
    QAction * m_action; // 右键菜单
    fileConfig config; // 配置
signals:
    void is_login(bool);
};

#endif // MANAGEMENT_H
